#! /usr/bin/python
# -*- coding:utf-8 -*-

## ========================================================================
## Copyright (C) 2007-2013  Jean-Philippe GOLAY
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
## ========================================================================

from product import *
from sale import *
import cgi

form = cgi.FieldStorage()
msg=""

print u"Content-type: text/plain"
print

if form.has_key(u"session"): 
  session = form[u"session"].value

if form.has_key(u"num"): 
  num = int(form[u"num"].value)
else:
  msg+=u"<br/>***Manque le nombre de produit !***"

idList=[]

while num > 0:
  idList.append(form["product_id%s" % (num - 1)].value)
  num -= 1

# GET BILL
prod=Product()
total_price = 0
total_tax = 0
html = "<table border='0' cellspacing='0' cellpadding='0' align='center'><tr><td class='text_left'><b>R&eacute;f&eacute;rence</b></td><td class='text_left'><b>D&eacute;signation</b></td><td><b>Quantit&eacute;</b></td><td class='text_right'><b>Taxes</b></td><td class='text_right'><b>Prix (HT)</b></td></tr>"

for id in idList:
  p = prod.read(id)
  total_price += float(p[u'prod_price'])
  total_tax += ( (float(p[u'prod_price'])/100) * float(p[u'prod_tax']) )
  html += "<tr><td class='text_left'>%s</td><td class='text_left'>%s</td><td>1</td><td class='text_right'>%s</td><td class='text_right'>%s&euro;</td></tr>" % ( p[u'prod_ref'], p[u'prod_desig'], p[u'prod_tax'], p[u'prod_price']  )

total = total_price + total_tax

html += "<tr><td colspan='4' class='text_right'>Total HT : </td><td class='text_right'>%.2f&euro;</td></tr><tr><td colspan='4' class='text_right'>Total Taxes : </td><td class='text_right'>%.2f&euro;</td></tr><tr><td colspan='4' class='text_right'><b>Total TTC : </b></td><td class='text_right'><b>%.2f&euro;</b></td></tr></table>" % ( total_price, total_tax, total )

ls=session.split('X')

sale=Sale()
bill_id=sale.new( ls[1], html, round(total, 2), "", "", "commande@a.confirmer", "", "", "" )

if bill_id == 0:
  msg+=u"<br/>Erreur impossible d'enregister la commande !"
  print msg
else:
  bill=sale.read(bill_id)
  if bill == 0:
    msg+=u"<br/>Erreur impossible de relire la commande !"
    print msg
  else:
    print "<b>Commande : %s</b><br/><div id='caddy'>%s</div><br/><div id='confirm_payment'>Completer le formulaire pour valider votre commande.<br/><div id='msg_box'></div><br/><table border='0' cellspacing='0' cellpadding='0' align='center'><tr><td class='adminTitle'>Nom et Pr&eacute;nom :</td></tr><tr><td class='adminField'><input type='text' size='24' maxlength='150' id='name'></td></tr><tr><td class='adminTitle'>Adresse (rue, n°, ville, cp) :</td></tr><tr><td class='adminField'><textarea rows='3' cols='23' id='address'></textarea></td></tr><tr><td class='adminTitle'>Email :</td></tr><tr><td class='adminField'><input type='text' size='24' maxlength='150' id='email'></td></tr><tr><td class='adminTitle'>T&eacute;l&eacute;phone :</td></tr><tr><td class='adminField'><input type='text' size='24' maxlength='16' id='phone'></td></tr><tr><td class='adminTitle'>Commentaire :</td></tr><tr><td class='adminField'><textarea rows='3' cols='23' id='note'></textarea></td></tr></table><br/><input type='hidden' id='sale_id' value='%s'><input type='button' onclick='confirmPayment()' value='Confirmer' title='Confirmer la commande'></div>" % ( bill['sale_date'], html, bill_id )
